//
//  registerController.m
//  pacc
//
//  Created by 测试 on 2018/5/23.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "registerController.h"

@interface registerController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *uNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *hospitalNameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *yanzhengmaTF;
@property (weak, nonatomic) IBOutlet UITextField *nimaTF;
@property (weak, nonatomic) IBOutlet UITextField *nmimaTF;
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,copy)NSMutableArray * dataSouse;
@property (nonatomic, strong)   UIView *maskLayer;
@property (weak, nonatomic) IBOutlet UIView *maskLayer2;
@property (nonatomic,strong)UIPickerView * pickerView;//自定义pickerview
@property (strong, nonatomic) UIView *sureview;
@property (nonatomic,strong)NSArray * letter;//保存要展示的字母
@property (nonatomic,strong)NSArray * number;
@end

@implementation registerController
- (IBAction)mimingwenBtn:(UIButton *)sender {
    _nimaTF.secureTextEntry = !_nimaTF.secureTextEntry;
    if (_nimaTF.secureTextEntry) {
        [sender setImage:[UIImage imageNamed:@"eye_off"] forState:UIControlStateNormal];
    }else          [sender setImage:[UIImage imageNamed:@"eye_on"] forState:UIControlStateNormal];

    
}
- (IBAction)mimingwenBtn2:(UIButton*)sender {
    _nmimaTF.secureTextEntry = !_nmimaTF.secureTextEntry;
    if (_nmimaTF.secureTextEntry) {
        [sender setImage:[UIImage imageNamed:@"eye_off"] forState:UIControlStateNormal];

        
    }else          [sender setImage:[UIImage imageNamed:@"eye_on"] forState:UIControlStateNormal];

    

}
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)choseHosName:(UIButton*)sender {
    [self.view endEditing:YES];
   _sureview.hidden = _pickerView.hidden = !_pickerView.hidden;
    if (_pickerView.hidden) {
        [self action_removeMask];
    }else {
        long index =  [self.pickerView selectedRowInComponent:0];
        [self pickerView:self.pickerView didSelectRow:index inComponent:0];
        [self addMaskLayer];
    }
}
- (void)action_removeMask{
    // 移除全屏点击事件
    [_maskLayer removeFromSuperview];
}
- (void)addMaskLayer{
    // 添加遮罩
    _maskLayer = [UIView  new];
    [_maskLayer setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-266)];
    [_maskLayer setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
    [self.view addSubview:_maskLayer];
    [self.view addSubview:_pickerView];
}
-(void)loadData{
    NSMutableDictionary *prama = [NSMutableDictionary  dictionary];
    [prama setValue:@100 forKey:@"size"];
    [prama setValue:@1 forKey:@"page"];
    [NetWorking startPOSTRequestWithUrl:@"hospitallist" parameters:prama logMes:@"医院列表" show:NO success:^(NSDictionary *data) {
        NSArray * hosArr = [data objectForKey:@"data"];

        NSMutableArray * temArr = [[NSMutableArray alloc]init];
    
        for (NSDictionary*nameDic in hosArr) {
            NSString *name = [nameDic objectForKey:@"name"];
            [temArr addObject:name];
        }
        self.letter = [temArr mutableCopy];
        [self.pickerView reloadAllComponents];
    } failure:^(NSError *error) {
        
    }];
}
-(void)creatPickView{
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, ScreenHeight-216, ScreenWidth, 216)];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.backgroundColor = baseBackgroundColor;
//    [self.view addSubview:self.pickerView];
    self.pickerView.hidden = YES;
    self.sureview = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-216-50, ScreenWidth, 50)];
    self.sureview.backgroundColor = [UIColor whiteColor];
    self.sureview.hidden = self.pickerView.hidden;
    [self.view addSubview:self.sureview];
    
    UIButton *sureBtn =[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-50-ZTbigmargin,0 , 50, 50)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:baseBlueColor forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchDown];
    [self.sureview addSubview:sureBtn];
    
    UIButton *cancelBtn =[[UIButton alloc]initWithFrame:CGRectMake(ZTbigmargin,0 , 50, 50)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:baseBlueColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchDown];
//    [self.sureview addSubview:cancelBtn];
}
-(void)sureClick{
    _sureview.hidden = _pickerView.hidden = !_pickerView.hidden;
    [self action_removeMask];
}
#pragma mark UIPickerView DataSource Method 数据源方法

//指定pickerview有几个表盘
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger result = 0;
    result = self.letter.count;
    return result;
}

#pragma mark UIPickerView Delegate Method 代理方法

//指定每行如何展示数据（此处和tableview类似）
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString * title = nil;
    title = self.letter[row];
    return title;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//    [self action_removeMask];
//    _sureview.hidden = _pickerView.hidden = !_pickerView.hidden;
    NSString * str = [NSString stringWithFormat:@"%@", self.letter[row]];
    _hospitalNameTF.text = str;
}
#pragma mark -- tableview --
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSouse.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",_dataSouse[indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    _hospitalNameTF.text = cell.textLabel.text;
    _sureview.hidden = _pickerView.hidden = YES;
    [self action_removeMask];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    if (_dataSouse) {
        _dataSouse = [[NSMutableArray alloc]init];
    }
    [self creatPickView];
    [self loadData];
    _dataSouse = [NSMutableArray arrayWithObjects:@"中山医院", nil];
    // Do any additional setup after loading the view from its nib.
}

//- (void)action_removeMask{
//    // 移除全屏点击事件
//    [maskLayer removeFromSuperview];
//}
//- (void)addMaskLayer{
//
//    // 添加遮罩
//    maskLayer = [UIView  new];
//    [maskLayer setFrame:[[UIScreen mainScreen] bounds]];
//    [maskLayer setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
//    [self.view addSubview:maskLayer];
//}

-(void)setSvp{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:RGB(85, 85, 85)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}
- (IBAction)registerClick:(id)sender {
   
    if (_phoneTf.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        [self setSvp];
        return;
    }
    if (!_uNameLabel.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的信息"];
        [self setSvp];

        return;
    }
    if (!_hospitalNameTF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的信息"];
        [self setSvp];

        return;
    }
    if (!_yanzhengmaTF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的信息"];
        [self setSvp];

        return;
    }
    if (!_nimaTF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的信息"];
        [self setSvp];

        return;
    }
    if (![_nmimaTF.text isEqualToString:_nimaTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"两次密码不一致"];
        [self setSvp];

        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:_phoneTf.text forKey:@"phone"];
    [dic setValue:_hospitalNameTF.text forKey:@"userDesc"];
    [dic setValue:_uNameLabel.text forKey:@"userName"];
    [dic setValue:_nimaTF.text forKey:@"userPassword"];
    [dic setValue:_yanzhengmaTF.text forKey:@"code"];

    NSString *url = [NSString stringWithFormat:@"userregister"];
    [NetWorking startPOSTRequestWithUrl:url parameters:dic logMes:@"注册" show:YES success:^(NSDictionary *data) {
        NSString *status = [NSString stringWithFormat:@"%@",[data objectForKey:@"status"]];
        if ([status isEqualToString:@"200"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        }else{
            [SVProgressHUD showErrorWithStatus:@"注册失败"];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"连接超时"];
    }];
}
/**
 *  键盘将要显示
 *
 *  @param notification 通知
 */
-(void)keyboardWillShow:(NSNotification *)notification
{
    //这样就拿到了键盘的位置大小信息frame，然后根据frame进行高度处理之类的信息
    CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    NSLog(@"frame显示==%@",NSStringFromCGRect(frame));
    
    _myScrollView.contentSize = CGSizeMake(0, 967);
    if (ScreenWidth <375) {
       
    }else{
        
    }
   
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
    
    NSLog(@"firstResponder%@",firstResponder);
    
}
/**
 *  键盘将要隐藏
 *
 *  @param notification 通知
 */
-(void)keyboardWillHidden:(NSNotification *)notification
{
//    _myScrollView.scrollEnabled = YES;
    CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSLog(@"frame隐藏==%@",NSStringFromCGRect(frame));
    _myScrollView.contentOffset = CGPointMake(0,  0);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
//    _sureview.hidden = _pickerView.hidden = !_pickerView.hidden;
//    [self action_removeMask];
}
- (IBAction)getCode:(UIButton*)sender {
    [NetWorking getCode:_phoneTf.text btn:sender];
}
-(void)viewDidAppear:(BOOL)animated{
    _myScrollView.contentSize = CGSizeMake(0, 667+216);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
